package cn.iocoder.yudao.module.system.controller.admin.userMenu;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.userMenu.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.userMenu.UserMenuDO;
import cn.iocoder.yudao.module.system.convert.userMenu.UserMenuConvert;
import cn.iocoder.yudao.module.system.service.userMenu.UserMenuService;

@Api(tags = "管理后台 - 用户和菜单关联")
@RestController
@RequestMapping("/system/user-menu")
@Validated
public class UserMenuController {

    @Resource
    private UserMenuService userMenuService;

    @PostMapping("/create")
    @ApiOperation("创建用户和菜单关联")
    @PreAuthorize("@ss.hasPermission('system:user-menu:create')")
    public CommonResult<Long> createUserMenu(@Valid @RequestBody UserMenuCreateReqVO createReqVO) {
        return success(userMenuService.createUserMenu(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新用户和菜单关联")
    @PreAuthorize("@ss.hasPermission('system:user-menu:update')")
    public CommonResult<Boolean> updateUserMenu(@Valid @RequestBody UserMenuUpdateReqVO updateReqVO) {
        userMenuService.updateUserMenu(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除用户和菜单关联")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('system:user-menu:delete')")
    public CommonResult<Boolean> deleteUserMenu(@RequestParam("id") Long id) {
        userMenuService.deleteUserMenu(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得用户和菜单关联")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('system:user-menu:query')")
    public CommonResult<UserMenuRespVO> getUserMenu(@RequestParam("id") Long id) {
        UserMenuDO userMenu = userMenuService.getUserMenu(id);
        return success(UserMenuConvert.INSTANCE.convert(userMenu));
    }

    @GetMapping("/list")
    @ApiOperation("获得用户和菜单关联列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('system:user-menu:query')")
    public CommonResult<List<UserMenuRespVO>> getUserMenuList(@RequestParam("ids") Collection<Long> ids) {
        List<UserMenuDO> list = userMenuService.getUserMenuList(ids);
        return success(UserMenuConvert.INSTANCE.convertList(list));
    }

    @PostMapping("/page")
    @ApiOperation("获得用户和菜单关联分页")
    @PreAuthorize("@ss.hasPermission('system:user-menu:query')")
    public CommonResult<PageResult<UserMenuRespVO>> getUserMenuPage(@Valid @RequestBody UserMenuPageReqVO pageVO) {
        PageResult<UserMenuDO> pageResult = userMenuService.getUserMenuPage(pageVO);
        return success(UserMenuConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出用户和菜单关联 Excel")
    @PreAuthorize("@ss.hasPermission('system:user-menu:export')")
    @OperateLog(type = EXPORT)
    public void exportUserMenuExcel(@Valid UserMenuExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<UserMenuDO> list = userMenuService.getUserMenuList(exportReqVO);
        // 导出 Excel
        List<UserMenuExcelVO> datas = UserMenuConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "用户和菜单关联.xls", "数据", UserMenuExcelVO.class, datas);
    }

}
